package com.jxj.controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jxj.daoImpl.UserDaoImpl;
import com.jxj.entity.Demand;
import com.jxj.entity.Page;
import com.jxj.entity.Receive;
import com.jxj.entity.User;
import com.jxj.service.HelpServiceImpl;
import com.jxj.service.UserServiceImpl;




@Controller
public class HelpController {
	@Resource
	private HelpServiceImpl serviceImpl;
	
	@Autowired
	private UserServiceImpl userServiceImpl;
	@RequestMapping("addDemand")
	public String addDemand(HttpSession session ,HttpServletRequest request,HttpServletResponse response,Demand demand)throws IOException{
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		String xuehao = (String) session.getAttribute("xuehao");
		User user = userServiceImpl.findUser1(xuehao);		
		double price = Double.parseDouble(request.getParameter("price"));
		this.serviceImpl.addDemand(user,demand,xuehao,price);
		
		response.getWriter().write("<script> alert('提交成功');window.location='findDemand'</script>");
		return null;
		
	}
	
	@RequestMapping("findDemand")
	public String findDemand(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String number=request.getParameter("pageNum");			
		int pageNum=0;
		if(number==null || number.equals("")){
			pageNum=1;			
		}
		else{
			//pageNum=int id = Integer.parseInt(number);	
			}						
		List<Demand> list=this.serviceImpl.findAll(pageNum,100);
		
		//int count=this.serviceImpl.count();
		Page<Demand> p=new Page<Demand>(pageNum, 8);			
		//p.setTotalCount(count);//赋值给totalcount对象
		p.setList(list);
		request.setAttribute("page", p);
		request.getRequestDispatcher("allFiind.jsp").forward(request, response);
		return null;
		
	}
	@RequestMapping("addRecive")
	public String AddRecive(HttpServletRequest request,HttpServletResponse response,User user,Demand demand,Receive receive) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		int id = Integer.parseInt(request.getParameter("id"));		
		HttpSession session=request.getSession();
		String xuehao=(String) session.getAttribute("xuehao");		
		User idUser=this.userServiceImpl.findUser1(xuehao);		
		this.serviceImpl.updateAccept(demand);
		
		if (this.serviceImpl.findUdid(id)){
			this.serviceImpl.addRecive(idUser, demand, receive);	
			response.getWriter().write("<script> alter('需求接受成功');window.location='个人页面'</script>");
		}
		else {
			response.getWriter().write("<script> alert('当前需求已被接取');window.location='findDemand'</script>");
			
		}
				
		return null;		
	}
}
